package cn.jinjimi.base.util;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;

import java.util.List;


/**
 * 退出快捷工具
 * <p/>
 * Created by tony on 9/28/14.
 */
public class ExitUtil {
    private static long lastBackTs = 0;

    /**
     * 双击退出函数
     *
     * @param context
     */
    public static boolean exitForDoubleClick(Activity context) {
        if (lastBackTs == 0) {
            lastBackTs = System.currentTimeMillis();
            ToastUtils.toastMessage(context, "再按一次退出程序");
        } else {
            long current = System.currentTimeMillis();
            if (current - lastBackTs <= 2000) {
                lastBackTs = 0;
                if (context instanceof FragmentActivity) {
                    List<Fragment> fmList = ((FragmentActivity) context).getSupportFragmentManager().getFragments();
                    if (fmList != null && !fmList.isEmpty())
                        fmList.clear();
                }
                return true;

            } else {
                ToastUtils.toastMessage(context, "再按一次退出程序");
                lastBackTs = current;
            }

        }
        return false;
    }
}
